package com.service.impl;

import com.dto.GoodsDTO;
import com.entity.Goods;
import com.entity.GoodsEntity;
import com.mapper.GoodsEntityMapper;
import com.mapper.GoodsMapper;
import com.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xuan
 * @Date: 2024/7/1 11:18
 * @Version: 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
   private GoodsEntityMapper goodsEntityMapper;
    @Autowired
    private  GoodsMapper goodsMapper;
    @Override
    public List<GoodsEntity> findAll() {
        return goodsEntityMapper.selectAll(null);
    }

    @Override
    public GoodsEntity getByGid(Integer gid) {
        return goodsEntityMapper.selectByGid(gid);
    }

    @Override
    public List<GoodsDTO> findGoodsDTO() {
        //先查寻所有goodsEntity列表
        //再查询所有goods列表
        //把同一个goods筛选出来
       List<GoodsDTO> goodsDTOList =  goodsEntityMapper.selectAllAndPlace();
       List<Goods> goodsList =goodsMapper.findAll();
       //查询类型

        //进行匹配
        for (GoodsDTO goodsDTO : goodsDTOList) {
            //设置List<goods>,防止空指针异常
            goodsDTO.setChildren(new ArrayList<>());
            String goodsUniqueNo = goodsDTO.getGoodsUniqueNo();
            for (Goods goods : goodsList) {
                //标识码相同就添加
                boolean compareGoods = goods.getGoodsUniqueNo().equals(goodsUniqueNo);
                if(compareGoods){
                    goodsDTO.getChildren().add(goods);
                }
            }
        }
        return goodsDTOList;



    }
}
